<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserDoctor
 *
 * @author Administrator
 */
class UserDoctorModel extends Model {

    /**
     * 获得用户关注的医生
     * @param type $uid
     * @param type $categoryid
     * @return type
     */
    public function getUserDoctor($uid, $categoryid = 0) {
        $where = '';
        if ($categoryid > 0) {
            $where.=' and ud.categoryid=' . $categoryid;
        }
        $sql = "SELECT ud.*,hd.doctor_name,hd.info,h.city_id,h.hospital_name,hk.keshi_name,nw.title as city,nw.ptitle as province FROM ts_user_doctor ud 
                left join ts_base_hospital_doctor hd on ud.doctor_id=hd.doctor_id 
                left join ts_base_hospital h on hd.hospital_id=h.hospital_id 
                left join ts_base_hospital_keshi hk on hd.keshi_id=hk.keshi_id 
                left join ts_network nw on h.city_id=nw.id 
                  where ud.user_id=$uid" . $where;
        $arr = $this->query($sql);
        if (!is_array($arr) || empty($arr)) {
            return array();
        }
        $docid = '0';
        foreach ($arr as $key => $value) {
            if (isset($value['doctor_id']))
                $docid.=',' . intval($value['doctor_id']);
        }
        if ($docid === '0') {
            return array();
        }
        $sql = 'SELECT doctor_id,avg(score) as score FROM ts_discuz_doctor  where doctor_id in (' . $docid . ') group by doctor_id;;';
        $docarr = $this->query($sql);
        $newarr = array();
        foreach ($docarr as $key => $value) {
            $newarr[$value['doctor_id']] = intval($value['score']);
        }
        return array('data' => $arr, 'score' => $newarr);
    }
    /**
     * 医生关注排序
     * @param type $categoryid
     * @param type $scoremin
     * @param type $scoremax
     * @param type $city
     * @return type
     */
    public function sortDoctor($categoryid, $scoremin = 0, $scoremax = 6, $city = 0) {
        $sql = 'SELECT doctor_id,count(doctor_id) as num FROM elssns.ts_user_doctor where categoryId=' . $categoryid . ' group by doctor_id;';
        $arr = $this->query($sql);
        if (!is_array($arr) || empty($arr)) {
            return array();
        }
        $docnum=array();
        foreach ($arr as $key => $value) {
            $docnum[$value['doctor_id']]=$value['num'];
        }
        $idarr=  array_keys($docnum);
        //分数过了
        if ($scoremin > 0 || $scoremax < 6) {
            $sql = 'SELECT doctor_id FROM ts_discuz_doctor  where doctor_id in (' . implode(',', $idarr) . ') and score between ' . $scoremin . ' and ' . $scoremax . ';';
            $arr = $this->query($sql);
            if (!is_array($arr) || empty($arr)) {
                return array();
            }
            $idarr = array();
            foreach ($arr as $key => $value) {
                $idarr[]=$value['doctor_id'];
            }
        }
        if($city==0){
            $sql="SELECT * FROM elssns.ts_base_hospital_doctor where doctor_id in (".implode(',', $idarr).")";
        }
        else{
            $sql="SELECT hd.* FROM elssns.ts_base_hospital_doctor hd 
                    left join ts_base_hospital h on hd.hospital_id=h.hospital_id 
                    where hd.doctor_id in (".implode(',', $idarr).") and h.city_id =".$city.";";
        }
        $arrs=  $this->query($sql);
        if(!is_array($arrs)||empty($arrs)) return array();
        return array('doctor'=>$arrs,'num'=>$docnum);
    }

}
